﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NewLife.Log;
using NewLife.Web;
using NewsPub;
using NewLife.CommonEntity;

public partial class NewsPub_DetailsForm : MyEntityForm<Details>
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        EntityForm.OnSetForm += EntityForm_OnSetForm;
        EntityForm.OnGetForm += EntityForm_OnGetForm;
        //EntityForm.OnSaveSuccess += EntityForm_OnSaveSuccess;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            //if (Entity.ID > 0) Entity.EditTime = DateTime.Now;
            writemap();
            


        }
    }
    void EntityForm_OnSetForm(object sender, EntityFormEventArgs e)
    {
        ////判断插入还是更新
        //if (Entity.ID > 0) { frmEditTime.Text = DateTime.Now.ToString(); isAdd = false; }
        //else
        //{
        //    frmCreateTime.Text = frmEditTime.Text = DateTime.Now.ToString();
        //    frmUserID.Text = Manager.Current.ID.ToString();
        //    isAdd = true;
        //}
        //if (Entity.ID > 0) Entity.EditTime = DateTime.Now;
        foreach (Classis c in Classis.FindAllChildsByParent(0))
        {
            if (c.Childs.Count > 0 || c.Name == "首页")
            {
                ListItem i = frmClassisID.Items.FindByValue(c.ID.ToString());

                i.Attributes.Add("style", "color:gray;");

                i.Attributes.Add("disabled", "true");
            }
        }
        //ListItem i = frmClassisID.Items.FindByValue("0");

        //    i.Attributes.Add("style", "color:gray;");

        //    i.Attributes.Add("disabled", "true");

        //    i.Value = "-1";
    }
    void EntityForm_OnGetForm(object sender, EntityFormEventArgs e)
    {

        if (Entity.ID > 0) Entity.EditTime = DateTime.Now;
    }
    void EntityForm_OnSaveSuccess(object sender, EntityFormEventArgs e)
    {
        
        writemap();
    }
    protected void writemap()
    {
        string fullFileName = "http://www.fjyxepb.gov.cn";
        string strLoc;
        string siteMapPath = Server.MapPath("~/sitemap.xml");

        FileInfo XMLFile = null;
        StreamWriter writerXMLFile = null;
        XMLFile = new FileInfo(siteMapPath);

        writerXMLFile = XMLFile.CreateText();
        //添加sitMap的头 
        writerXMLFile.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        writerXMLFile.WriteLine("<urlset");
        writerXMLFile.WriteLine("xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"");
        writerXMLFile.WriteLine("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
        writerXMLFile.WriteLine("xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9");
        writerXMLFile.WriteLine("http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">");
        //数据 
        //首页
        writerXMLFile.WriteLine("<url>");
        writerXMLFile.WriteLine("<loc>" + fullFileName + "</loc>");
        writerXMLFile.WriteLine("<priority>" + (1.0).ToString("0.00") + "</priority>");
        writerXMLFile.WriteLine("<lastmod>" + DateTime.Now.ToString("yyyy-MM-dd") + "</lastmod>");
        writerXMLFile.WriteLine("<changefreq>daily</changefreq>");
        writerXMLFile.WriteLine("</url>");
        //分类

        foreach (Classis news in Classis.FindAllByParent(0))
        {
            strLoc = fullFileName + "/list.aspx?id=" + news.ID;
            writerXMLFile.WriteLine("<url>");
            writerXMLFile.WriteLine("<loc>" + strLoc + "</loc>");
            writerXMLFile.WriteLine("<priority>" + 0.6 + "</priority>");
            writerXMLFile.WriteLine("<lastmod>" + DateTime.Now.ToString("yyyy-MM-dd") + "</lastmod>");
            writerXMLFile.WriteLine("<changefreq>weekly</changefreq>");
            writerXMLFile.WriteLine("</url>");
        }
        //记录

        foreach (Details news in Details.FindAll())
        {
            strLoc = fullFileName + "/disp.aspx?id=" + news.ID;
            writerXMLFile.WriteLine("<url>");
            writerXMLFile.WriteLine("<loc>" + strLoc + "</loc>");
            writerXMLFile.WriteLine("<lastmod>" + news.EditTime.ToString("yyyy-MM-dd") + "</lastmod>");
            writerXMLFile.WriteLine("<changefreq>weekly</changefreq>");
            writerXMLFile.WriteLine("<priority>" + 0.5 + "</priority>");
            writerXMLFile.WriteLine("</url>");
        }
        //添加sitMap的尾 

        //千万不能忘了这句 
        writerXMLFile.WriteLine("</urlset>");

        writerXMLFile.Close();
        writerXMLFile.Dispose();
    }
}